package main

import (
	"fmt"
	"time"

	"LogManagementSystem/logagent/kafka"
	"LogManagementSystem/logagent/mainfuncs"
	"LogManagementSystem/logagent/tailf"

	"github.com/astaxie/beego/logs"
)

func main() {
	//读配置
	filename := "/Users/fcou/go/src/LogManagementSystem/logagent/conf/logagent.conf"
	err := mainfuncs.LoadConf("ini", filename)
	if err != nil {
		fmt.Printf("load conf failed, err:%v\n", err)
		panic("load conf failed")
		return
	}
	//启动本系统日志记录功能，出现任何问题进行记录
	err = mainfuncs.InitLogger()
	if err != nil {
		fmt.Printf("load logger failed, err:%v\n", err)
		panic("load logger failed")
		return
	}
	logs.Debug("load conf succ, config:%v", mainfuncs.AppConfig)

	//初始化ETCD,获取要收集日志的信息
	collectConf, err := mainfuncs.InitEtcd(mainfuncs.AppConfig.EtcdAddr, mainfuncs.AppConfig.EtcdKey)
	if err != nil {
		logs.Error("init etcd failed, err:%v", err)
		return
	}
	logs.Debug("initialize etcd succ")

	//逐行读取日志功能初始化
	err = tailf.InitTail(collectConf, mainfuncs.AppConfig.ChanSize)
	if err != nil {
		logs.Error("init tail failed, err:%v", err)
		return
	}
	logs.Debug("initialize tailf succ")

	//初始化kafka
	err = kafka.InitKafka(mainfuncs.AppConfig.KafkaAddr)
	if err != nil {
		logs.Error("init tail failed, err:%v", err)
		return
	}
	logs.Debug("initialize all succ") //初始化全部结束

	go func() {
		var count int
		for {
			count++
			logs.Debug("test for logger %d", count)
			time.Sleep(time.Millisecond * 2000)
		}
	}()

	//执行主处理逻辑：
	//一行一行读要收集的日志，变成消息
	//建立kafka消息产生者，初始化，发送之前的消息
	//日志收集客户端完成
	err = mainfuncs.ServerRun()
	if err != nil {
		logs.Error("serverRUn failed, err:%v", err)
		return
	}

	logs.Info("program exited")

	mainfuncs.SendToWeixin() //立即微信通知：程序退出

}
